package com.shxzz.easy12306.services.ticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shxzz.easy12306.framework.starter.cache.StringRedisTemplateProxy;
import com.shxzz.easy12306.services.ticket.common.constant.RedisKeyConstant;
import com.shxzz.easy12306.services.ticket.common.constant.TicketConstant;
import com.shxzz.easy12306.services.ticket.dao.entity.TrainStationDO;
import com.shxzz.easy12306.services.ticket.dao.mapper.TrainStationMapper;
import com.shxzz.easy12306.services.ticket.dto.domain.RouteDTO;
import com.shxzz.easy12306.services.ticket.dto.resp.TrainStationQueryRespDTO;
import com.shxzz.easy12306.services.ticket.service.TrainStationService;
import com.shxzz.easy12306.services.ticket.toolkit.StationCalculateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 列车站点接口实现
 *
 * @author shxzz
 * @create 2024-03-05-23:36
 */
@RequiredArgsConstructor
@Service
public class TicketStationServiceImpl implements TrainStationService {

    private final TrainStationMapper trainStationMapper;
    private final StringRedisTemplateProxy stringRedisTemplateProxy;

    @Override
    public List<RouteDTO> listTrainStationRoute(String trainId, String startStation, String endStation) {
        return StationCalculateUtil.getThroughRouteList(getAllStation(trainId), startStation, endStation);
    }

    private List<String> getAllStation(String trainId) {
        LambdaQueryWrapper<TrainStationDO> queryWrapper = Wrappers.lambdaQuery(TrainStationDO.class)
                .eq(TrainStationDO::getTrainId, trainId)
                .select(TrainStationDO::getDeparture);
        List<TrainStationDO> list = trainStationMapper.selectList(queryWrapper);
        return list.stream().map(TrainStationDO::getDeparture).toList();
    }

    @Override
    public List<RouteDTO> listTakeoutTrainStationRoute(String trainId, String departure, String arrival) {
        return StationCalculateUtil.getTakeoutRouteList(getAllStation(trainId), departure, arrival);
    }

    @Override
    public List<TrainStationQueryRespDTO> listTrainStationQuery(String trainId) {
        String trainStopoverStationListStr = stringRedisTemplateProxy.safeGet(
                RedisKeyConstant.TRAIN_STOPOVER_STATION_INFO_PREFIX + trainId,
                String.class,
                () -> {
                    LambdaQueryWrapper<TrainStationDO> queryWrapper = Wrappers.lambdaQuery(TrainStationDO.class)
                            .eq(TrainStationDO::getTrainId, trainId);
                    List<TrainStationDO> actualTrainStationList = trainStationMapper.selectList(queryWrapper);
                    return JSON.toJSONString(BeanUtil.copyToList(actualTrainStationList, TrainStationQueryRespDTO.class));
                },
                TicketConstant.ADVANCED_QUERY_TICKET_DAY,
                TimeUnit.DAYS
        );
        return JSON.parseArray(trainStopoverStationListStr, TrainStationQueryRespDTO.class);
    }
}
